package com.heima.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.heima.mapper.RegionMapper;
import com.heima.pojo.Region;
import com.heima.pojo.dto.RegionDto;
import com.heima.pojo.vo.RegionVo;
import com.heima.service.RegionService;
import com.heima.utils.PageResult;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;


/**
 * @author 赵超
 */
@Service
@RequiredArgsConstructor
public class RegionServiceImpl extends ServiceImpl<RegionMapper, Region> implements RegionService {
    private final RegionMapper regionMapper;


    @Override
    public PageResult<RegionVo> page(Integer pageIndex, Integer pageSize, String name) {
        PageHelper.startPage(pageIndex, pageSize);
        Page<RegionVo> list = regionMapper.page(name);

        Integer totalPage = list.getPages();
        Long totalCount = list.getTotal();

        return new PageResult<>(pageIndex, pageSize, totalPage, totalCount, list);
    }

    @Override
    public Integer add(RegionDto regionDto) {
        Region region = new Region();
        BeanUtils.copyProperties(regionDto, region);
        region.setName(regionDto.getRegionName());
        return regionMapper.insert(region);
    }

    @Override
    public Integer update(Long regionId, RegionDto regionDto) {
        Region region = new Region();
        BeanUtils.copyProperties(regionDto, region);
        region.setName(regionDto.getRegionName());
        region.setId(regionId);
        return regionMapper.updateById(region);
    }

    @Override
    public Integer delete(Long id) {
        return regionMapper.deleteById(id);
    }


}
